﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using SaleManagement.Domains;
using SaleManagement.Data;

#endregion

namespace SaleManagement.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ImportDetailProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ImportDetailProviderBaseCore : EntityProviderBase<SaleManagement.Domains.ImportDetail, SaleManagement.Domains.ImportDetailKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, SaleManagement.Domains.ImportDetailKey key)
		{
			return Delete(transactionManager, key.Id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _id)
		{
			return Delete(null, _id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _id);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		FK_tblImportDetail_tblImport Description: 
		/// </summary>
		/// <param name="_importId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByImportId(System.Int32 _importId)
		{
			int count = -1;
			return GetByImportId(_importId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		FK_tblImportDetail_tblImport Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_importId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		/// <remarks></remarks>
		public TList<ImportDetail> GetByImportId(TransactionManager transactionManager, System.Int32 _importId)
		{
			int count = -1;
			return GetByImportId(transactionManager, _importId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		FK_tblImportDetail_tblImport Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_importId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByImportId(TransactionManager transactionManager, System.Int32 _importId, int start, int pageLength)
		{
			int count = -1;
			return GetByImportId(transactionManager, _importId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		fkTblImportDetailTblImport Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_importId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByImportId(System.Int32 _importId, int start, int pageLength)
		{
			int count =  -1;
			return GetByImportId(null, _importId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		fkTblImportDetailTblImport Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_importId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByImportId(System.Int32 _importId, int start, int pageLength,out int count)
		{
			return GetByImportId(null, _importId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblImport key.
		///		FK_tblImportDetail_tblImport Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_importId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public abstract TList<ImportDetail> GetByImportId(TransactionManager transactionManager, System.Int32 _importId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		FK_tblImportDetail_tblProduct Description: 
		/// </summary>
		/// <param name="_productId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByProductId(System.Int32 _productId)
		{
			int count = -1;
			return GetByProductId(_productId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		FK_tblImportDetail_tblProduct Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		/// <remarks></remarks>
		public TList<ImportDetail> GetByProductId(TransactionManager transactionManager, System.Int32 _productId)
		{
			int count = -1;
			return GetByProductId(transactionManager, _productId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		FK_tblImportDetail_tblProduct Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByProductId(TransactionManager transactionManager, System.Int32 _productId, int start, int pageLength)
		{
			int count = -1;
			return GetByProductId(transactionManager, _productId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		fkTblImportDetailTblProduct Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_productId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByProductId(System.Int32 _productId, int start, int pageLength)
		{
			int count =  -1;
			return GetByProductId(null, _productId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		fkTblImportDetailTblProduct Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_productId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public TList<ImportDetail> GetByProductId(System.Int32 _productId, int start, int pageLength,out int count)
		{
			return GetByProductId(null, _productId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblImportDetail_tblProduct key.
		///		FK_tblImportDetail_tblProduct Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.ImportDetail objects.</returns>
		public abstract TList<ImportDetail> GetByProductId(TransactionManager transactionManager, System.Int32 _productId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override SaleManagement.Domains.ImportDetail Get(TransactionManager transactionManager, SaleManagement.Domains.ImportDetailKey key, int start, int pageLength)
		{
			return GetById(transactionManager, key.Id, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_tblImportDetail index.
		/// </summary>
		/// <param name="_id"></param>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public SaleManagement.Domains.ImportDetail GetById(System.Int32 _id)
		{
			int count = -1;
			return GetById(null,_id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblImportDetail index.
		/// </summary>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public SaleManagement.Domains.ImportDetail GetById(System.Int32 _id, int start, int pageLength)
		{
			int count = -1;
			return GetById(null, _id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblImportDetail index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public SaleManagement.Domains.ImportDetail GetById(TransactionManager transactionManager, System.Int32 _id)
		{
			int count = -1;
			return GetById(transactionManager, _id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblImportDetail index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public SaleManagement.Domains.ImportDetail GetById(TransactionManager transactionManager, System.Int32 _id, int start, int pageLength)
		{
			int count = -1;
			return GetById(transactionManager, _id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblImportDetail index.
		/// </summary>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public SaleManagement.Domains.ImportDetail GetById(System.Int32 _id, int start, int pageLength, out int count)
		{
			return GetById(null, _id, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblImportDetail index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.ImportDetail"/> class.</returns>
		public abstract SaleManagement.Domains.ImportDetail GetById(TransactionManager transactionManager, System.Int32 _id, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _tblImportDetail_SearchByImportId 
		
		/// <summary>
		///	This method wrap the '_tblImportDetail_SearchByImportId' stored procedure. 
		/// </summary>
		/// <param name="importId"> A <c>System.Int32?</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet SearchByImportId(System.Int32? importId)
		{
			return SearchByImportId(null, 0, int.MaxValue , importId);
		}
		
		/// <summary>
		///	This method wrap the '_tblImportDetail_SearchByImportId' stored procedure. 
		/// </summary>
		/// <param name="importId"> A <c>System.Int32?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet SearchByImportId(int start, int pageLength, System.Int32? importId)
		{
			return SearchByImportId(null, start, pageLength , importId);
		}
				
		/// <summary>
		///	This method wrap the '_tblImportDetail_SearchByImportId' stored procedure. 
		/// </summary>
		/// <param name="importId"> A <c>System.Int32?</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet SearchByImportId(TransactionManager transactionManager, System.Int32? importId)
		{
			return SearchByImportId(transactionManager, 0, int.MaxValue , importId);
		}
		
		/// <summary>
		///	This method wrap the '_tblImportDetail_SearchByImportId' stored procedure. 
		/// </summary>
		/// <param name="importId"> A <c>System.Int32?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public abstract DataSet SearchByImportId(TransactionManager transactionManager, int start, int pageLength , System.Int32? importId);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;ImportDetail&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;ImportDetail&gt;"/></returns>
		public static TList<ImportDetail> Fill(IDataReader reader, TList<ImportDetail> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				SaleManagement.Domains.ImportDetail c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("ImportDetail")
					.Append("|").Append((System.Int32)reader[((int)ImportDetailColumn.Id - 1)]).ToString();
					c = EntityManager.LocateOrCreate<ImportDetail>(
					key.ToString(), // EntityTrackingKey
					"ImportDetail",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new SaleManagement.Domains.ImportDetail();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.Id = (System.Int32)reader[((int)ImportDetailColumn.Id - 1)];
					c.ImportId = (System.Int32)reader[((int)ImportDetailColumn.ImportId - 1)];
					c.ProductId = (System.Int32)reader[((int)ImportDetailColumn.ProductId - 1)];
					c.Quantity = (System.Int32)reader[((int)ImportDetailColumn.Quantity - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="SaleManagement.Domains.ImportDetail"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.ImportDetail"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, SaleManagement.Domains.ImportDetail entity)
		{
			if (!reader.Read()) return;
			
			entity.Id = (System.Int32)reader[((int)ImportDetailColumn.Id - 1)];
			entity.ImportId = (System.Int32)reader[((int)ImportDetailColumn.ImportId - 1)];
			entity.ProductId = (System.Int32)reader[((int)ImportDetailColumn.ProductId - 1)];
			entity.Quantity = (System.Int32)reader[((int)ImportDetailColumn.Quantity - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="SaleManagement.Domains.ImportDetail"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.ImportDetail"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, SaleManagement.Domains.ImportDetail entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (System.Int32)dataRow["Id"];
			entity.ImportId = (System.Int32)dataRow["ImportId"];
			entity.ProductId = (System.Int32)dataRow["ProductId"];
			entity.Quantity = (System.Int32)dataRow["Quantity"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.ImportDetail"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">SaleManagement.Domains.ImportDetail Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, SaleManagement.Domains.ImportDetail entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ImportIdSource	
			if (CanDeepLoad(entity, "Import|ImportIdSource", deepLoadType, innerList) 
				&& entity.ImportIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ImportId;
				Import tmpEntity = EntityManager.LocateEntity<Import>(EntityLocator.ConstructKeyFromPkItems(typeof(Import), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ImportIdSource = tmpEntity;
				else
					entity.ImportIdSource = DataRepository.ImportProvider.GetById(transactionManager, entity.ImportId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ImportIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ImportIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ImportProvider.DeepLoad(transactionManager, entity.ImportIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ImportIdSource

			#region ProductIdSource	
			if (CanDeepLoad(entity, "Product|ProductIdSource", deepLoadType, innerList) 
				&& entity.ProductIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ProductId;
				Product tmpEntity = EntityManager.LocateEntity<Product>(EntityLocator.ConstructKeyFromPkItems(typeof(Product), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ProductIdSource = tmpEntity;
				else
					entity.ProductIdSource = DataRepository.ProductProvider.GetById(transactionManager, entity.ProductId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ProductIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ProductIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ProductProvider.DeepLoad(transactionManager, entity.ProductIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ProductIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the SaleManagement.Domains.ImportDetail object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">SaleManagement.Domains.ImportDetail instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">SaleManagement.Domains.ImportDetail Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, SaleManagement.Domains.ImportDetail entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ImportIdSource
			if (CanDeepSave(entity, "Import|ImportIdSource", deepSaveType, innerList) 
				&& entity.ImportIdSource != null)
			{
				DataRepository.ImportProvider.Save(transactionManager, entity.ImportIdSource);
				entity.ImportId = entity.ImportIdSource.Id;
			}
			#endregion 
			
			#region ProductIdSource
			if (CanDeepSave(entity, "Product|ProductIdSource", deepSaveType, innerList) 
				&& entity.ProductIdSource != null)
			{
				DataRepository.ProductProvider.Save(transactionManager, entity.ProductIdSource);
				entity.ProductId = entity.ProductIdSource.Id;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ImportDetailChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>SaleManagement.Domains.ImportDetail</c>
	///</summary>
	public enum ImportDetailChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Import</c> at ImportIdSource
		///</summary>
		[ChildEntityType(typeof(Import))]
		Import,
			
		///<summary>
		/// Composite Property for <c>Product</c> at ProductIdSource
		///</summary>
		[ChildEntityType(typeof(Product))]
		Product,
		}
	
	#endregion ImportDetailChildEntityTypes
	
	#region ImportDetailFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ImportDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ImportDetail"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ImportDetailFilterBuilder : SqlFilterBuilder<ImportDetailColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ImportDetailFilterBuilder class.
		/// </summary>
		public ImportDetailFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ImportDetailFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ImportDetailFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ImportDetailFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ImportDetailFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ImportDetailFilterBuilder
	
	#region ImportDetailParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ImportDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ImportDetail"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ImportDetailParameterBuilder : ParameterizedSqlFilterBuilder<ImportDetailColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ImportDetailParameterBuilder class.
		/// </summary>
		public ImportDetailParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ImportDetailParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ImportDetailParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ImportDetailParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ImportDetailParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ImportDetailParameterBuilder
	
	#region ImportDetailSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ImportDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ImportDetail"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ImportDetailSortBuilder : SqlSortBuilder<ImportDetailColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ImportDetailSqlSortBuilder class.
		/// </summary>
		public ImportDetailSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ImportDetailSortBuilder
	
} // end namespace
